<?php
/**
*
*
*/
abstract class Yz_Application_Web
    extends Yz_Application_Abstract
{

    /**
     * @var Yz_Router_Interface&
     */
    protected static $router;


    /**
     *
     *  @return int
     */
    public static function main()
    {
        ob_start();
        return parent::main();
    }

    /** select router application-wide and store it for possible access
     *  @return Yz_Router_Interface
     */
    public static function selectRouter()
    {
        $router =& self::$router;
        if (null === $router) {
            $class = Yz::getAppNamespace() . '\\Router_Default';
            if (false === Yz::loadClass($class)) {
                $class = 'Yz_Router_Default';
            }
            $router = $class::getInstance();
        }
        return $router;
    }

    /**
     *
     *
     */
    protected function _main()
    {
        $rq = $this->initRequest();
        $rs = $this->initResponse();
        $rs = $this
            ->loadConfigForHost($rq->getHostname())
            ->selectRouter()
            ->routeRequest($rq, $rs)
        ;
        assert('$rs instanceof Yz_HTTP_Response_Interface');
        return $this->sendResponse($rs);
    }

    /**
     *
     */
    protected function sendResponse( Yz_HTTP_Response_Interface $rs )
    {
        $rs->send();
        return 0;
    }

    /** initialize HTTP Request object
     *  @return Yz_HTTP_Request_Interface
     */
    protected function initRequest()
    {
        return Yz_HTTP_Request::getInstance();
    }

    /** initialize HTTP Response object
     *  @return Yz_HTTP_Response_Interface
     */
    protected function initResponse()
    {
        return Yz_HTTP_Response::getInstance();
    }


    /**
     *
     */
    protected function displayErrorMessage( $message )
    {
        // dispose of any buffered data
        while( false !== ob_get_length() ) {
            ob_end_clean();
        } // while

        // ensure the message appears regardless of the status
        // of Response objects and current directory
        header_remove(); // remove all headers
        header( 'HTTP/1.1 500 Internal Server Error' );
        header( 'Content-Type: text/html; charset=utf-8' );
        header( 'Cache-Control: no-cache' );
        header( 'Last-Modified: ' . gmdate( 'r', YZ_TIME ) );
        header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
        include( YZ_LIB . 'Yz/templates/fatal-error.php' );
        return $this;
    }


    /** include host-specific config
     *  @param string host
     *  @return chainable
     */
    protected function loadConfigForHost($host)
    {
        $host = strtr( $host, ':', '-'); // assuming host name is valid
        debug("Reading config for host {$host}");

        // new
        $configString = include( "config.{$host}.php" );
        if ( "" !== "$configString" ) {
            $this->config = Yz_Config_Loader::load( $configString, 'App.Config.Host', $this->config );
        }
        return $this;
    }

} // class


